<?php 
class Model_Contactus extends Database
{
	
	function Model_Contactus()
	{
		$this->tableName			= CONTACT_US;
		$this->tableName1 			= ENQUIRY_REPLY;
		$this->tableName2 			= ADMINDETAILS;
		$this->usercontactus 		= USERCONTACTUS;
		$this->usercontactusreply 	= USERCONTACTUSREPLY;
		$this->contact_day_time 	= CONTACT_DAY_TIME;

		$this->Database();
	}
	function getReplydetailsByUser($id)
	{
		$fields = array();	
		$tables = array( $this->usercontactusreply );
		$result1 = $this->SelectData( $fields,$tables, $where = array("id='".$id."'"), $order ="", $group=array(),$limit = "",0,0 );
		$result= $this->FetchRow( $result1 );
		return $result;
	}
	function getContactusByUserReply($id)
	{
		$fields=array();
		$tables=array($this->usercontactus);
		$where=array("id='".$id."'");
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0);
		$result= $this->FetchRow($result1);
		return $result;		
	}
	function deleteReplyMessagesByuserReply($id)
	{
		$this->DeleteData($this->usercontactusreply,"id",$id);
	}
	function AllReplyMessagesByIdByUser($id,$search,$limit='',$offset='')
	{	
		$fields = array();	
		$tables = array($this->usercontactusreply);
		$clause="contact_id='".$id."'";
		if($search !="")
		{
			$clause.= " AND subject_reply like '%".$search."%'";
		}
		$where=array($clause);		
		
		$result1 = $this->SelectData( $fields,$tables, $where, $order=array(), $group=array(),$limit,$offset,0);
		$result= $this->FetchAll( $result1 );
		return $result;
	}
	function addContactUsReplyByUser($array)
	{
		$this->InsertData( $this->usercontactusreply , $array );		
		$insertId = mysql_insert_id();
		return $insertId;
	} 
	function addContactUsReplyByUserWithContactTime($array)
	{
		$this->InsertData( $this->contact_day_time , $array );		
		$insertId = mysql_insert_id();
		return $insertId;
	} 
	function editContactusByUser( $array , $id )
	{
		$this->UpdateData( $this->usercontactus , $array , "id" , $id );		
	}	
	function listRecordByUser($searchtxt, $limit="", $offset="")
	{
		 
		$fields = array();	
		$tables = array( $this->usercontactus);
		$where=array("user_id=".$_SESSION['userId']."");
		
		if($searchtxt==''){
			
		$where=array("user_id=".$_SESSION['userId']."");
			
		}else{
			$where[]= " subject LIKE '%".$searchtxt."%' OR name LIKE '%".$searchtxt."%' AND user_id=".$_SESSION['userId']."";	
		}
		$result1 = $this->SelectData( $fields,$tables, $where, $order = array( "id DESC" ), $group=array(),$limit,$offset,0);
		$result= $this->FetchAll( $result1 );
		return $result;
	}
	function deleteContactusByUser($Id)
	{
		$this->DeleteData($this->usercontactus,"id",$Id);
	}
	function getContactusByUser($id)
	{
		$fields=array();
		$tables=array($this->usercontactus);
		$where=array("id='".$id."'");
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0);
		$result= $this->FetchRow($result1);
		return $result;		
	}
	function getContactTimeByUser($id)
	{
		$fields=array();
		$tables=array($this->contact_day_time);
		$where=array("contact_id='".$id."'");
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0);
		$result= $this->FetchRow($result1);
		return $result;		
	}
	
	function addContactus( $array )
	{
		$this->InsertData( $this->tableName , $array );		
		$insertId = mysql_insert_id();
		return $insertId;
	}
	function addContactusUser( $array )
	{
		$this->InsertData( $this->usercontactus , $array );		
		$insertId = mysql_insert_id();
		return $insertId;
	}
	function editContactus( $array , $id )
	{
		$this->UpdateData( $this->tableName , $array , "id" , $id );		
		
	}	
	function updateContactUsStatus($ids, $status) {
		$sql = "UPDATE ".$this->tableName." SET readflag='".$status."' WHERE id IN (".$ids.")";
		$result1= $this->ExecuteQuery($sql);	 
		
	}
	function deleteEnquiry($ids) {
		$sql = "DELETE FROM ".$this->tableName." WHERE id IN (".$ids.")";
		$result1= $this->ExecuteQuery($sql);	 
	}
	function deleteReplies($ids) {
		$sql = "DELETE FROM ".$this->tableName1." WHERE id IN (".$ids.")";
		$result1= $this->ExecuteQuery($sql);	 
	}
	function listContactus( $val )
	{
		$fields = array();	
		$tables = array( $this->tableName );
		$where=array("");
		$result1 = $this->SelectData( $fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0 );
		$result= $this->FetchAll( $result1 );
		return $result;
	}
	function listContactusPaginate( $val, $limit , $offset )
	{
		$fields = array();	
		$tables = array( $this->tableName );
		$where=array("");
		$result1 = $this->SelectData( $fields,$tables, $where , $order = array(), $group=array(),$limit,$offset,0 );
		$result= $this->FetchAll( $result1 );
		return $result;
	}
	function listContactusAdmin( $searchArray,$orderarr )
	{
		$fields = array();	
		$tables = array( $this->tableName );
		$where=array("contactus_deleted_status  = 0 AND contactus_firstname LIKE '%".$val."%'");
		$result1 = $this->SelectData( $fields,$tables, $where, $orderarr , $group=array(),$limit = "",0,0);
		$result= $this->FetchAll( $result1 );
		return $result;
	}
	function listcontact($searchArray,$orderarr )
	{
		$type   = $searchArray['type'];
		$first_name   = $searchArray['contactus_firstname'];
		
		$fields = array();	
		$tables = array( $this->tableName);
		$clause="contactus_deleted_status  = '0' ";
		if($type !="")
		{
			$clause.= "AND contactus_type ='".$type."'";
		}
		if($first_name !="")
		{
			$clause.= " And contactus_firstname ='".$first_name."'";
			//$clause.= " ";
		}
		$where= array($clause);
		
		$result1 = $this->SelectData( $fields,$tables, $where, $orderarr , $group=array(),$limit = "",0,0);
		$result= $this->FetchAll( $result1 );
		return $result;
	}
	function listcontactPaginate($searchArray, $limit , $offset ,$orderarr)
	{
	    $type   = $searchArray['type'];
		$first_name   = $searchArray['contactus_firstname'];
		
		$fields = array();	
		$tables = array( $this->tableName);
		$clause="contactus_deleted_status  = '0' ";
		if($type !="")
		{
			$clause.= "AND contactus_type ='".$type."'";
		}
		if($first_name !="")
		{
			$clause.= " And contactus_firstname ='".$first_name."'";
			//$clause.= " ";
		}
		$where= array($clause);
		
		$fields = array();	
		$tables = array( $this->tableName );
		$where=array("contactus_deleted_status  = 0 AND contactus_firstname LIKE '%".$val."%'");
		$result1 = $this->SelectData( $fields,$tables, $where , $orderarr, $group=array(),$limit,$offset,0);
		$result= $this->FetchAll( $result1 );
		return $result;
	}
	function listContactusAdminPaginate( $val, $limit , $offset )
	{
		$fields = array();	
		$tables = array( $this->tableName );
		$where=array("contactus_deleted_status  = 0 AND contactus_firstname LIKE '%".$val."%'");
		$result1 = $this->SelectData( $fields,$tables, $where , $orderarr, $group=array(),$limit,$offset,0);
		$result= $this->FetchAll( $result1 );
		return $result;
	}
	function listContactusById( $val )
	{
		$fields = array();	
		$tables = array( $this->tableName );
		$where=array("");
		$result1 = $this->SelectData( $fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0 );
		$result= $this->FetchAll( $result1 );
		return $result;
	}
	function getContactus($id)
	{
		$fields=array();
		$tables=array($this->tableName);
		$where=array("id='".$id."'");
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0);
		$result= $this->FetchRow($result1);
		return $result;		
	}
	function statusContactus( $array , $id )
	{
		$this->UpdateData( $this->tableName , $array , "id" , $id );		
		
	}		
	function deleteContactus($Id)
	{
		$this->DeleteData($this->tableName,"id",$Id);
	}
	function search($searchArray,$orderarr)
	{	
		$type   = $searchArray['type'];
		$first_name   = $searchArray['contactus_firstname'];
		
		$fields = array();	
		$tables = array( $this->tableName);
		$clause="contactus_deleted_status  = '0' ";
		if($type !="")
		{
			$clause.= "AND contactus_type ='".$type."'";
		}
		if($first_name !="")
		{
			$clause.= " And contactus_firstname ='".$first_name."'";
			//$clause.= " ";
		}
		$where= array($clause);
		
		$result1 = $this->SelectData( $fields,$tables, $where,$orderarr, $group=array(),$limit = "",0,0);
		$result= $this->FetchAll( $result1 );
		return $result;
	}
   function searchPaginate($searchArray, $limit, $offset,$orderarr)
	{
		
		$type = $searchArray['type'];
		$first_name   = $searchArray['contactus_firstname'];	
		$fields = array();	
		$tables = array( $this->tableName);
		$clause="contactus_deleted_status  = '0' ";
		if($type !="")
		{
			$clause.= "AND contactus_type ='".$type."'";
		}
		if($first_name !="")
		{
			$clause.= " And contactus_firstname ='".$first_name."'";
			//$clause.= " ";
		}
		$where= array($clause);
		
		$result1 = $this->SelectData( $fields,$tables, $where, $orderarr, $group=array(),$limit,$offset,0);
		$result= $this->FetchAll( $result1 );
		return $result;
	}
	function listRecord1( )
	{
		$fields = array();	
		$tables = array( $this->tableName );
		$result1 = $this->SelectData( $fields,$tables, $where = array(), $order = array( "entry_on DESC" ), $group=array(),$limit = "",0,0 );
		$result= $this->FetchAll( $result1 );
		return $result;
	}
	function listRecord1User($id)
	{
		$fields = array();	
		$tables = array( $this->usercontactus );
		$result1 = $this->SelectData( $fields,$tables, $where = array("user_id=".$id.""), $order = array( "entry_on DESC" ), $group=array(),$limit = "",0,0 );
		$result= $this->FetchAll( $result1 );
		return $result;
	}
	function listRecordPaginate( $limit, $offset )
	{
		$fields = array();	
		$tables = array( $this->tableName );
		$result1 = $this->SelectData( $fields,$tables, $where = array(), $order = array( "entry_on  DESc" ), $group=array(),$limit,$offset,0 );
		$result= $this->FetchAll( $result1 );
		return $result;
	}
	function CountUnread( )
	{
		$fields = array();	
		$tables = array( $this->tableName );
		$result1 = $this->SelectData( $fields,$tables, $where = array("readflag = 'unread'"), $order = array( ), $group=array(),$limit = "",0,0); 
		$result= $this->FetchAll( $result1 );
		return $result;
	}
	
	function getEmail1()
	{
	    $sql="SELECT `email` FROM `ebay_admindetails` WHERE `id`=1";
		$res=$this->ExecuteQuery($sql); 
		$row=$this->FetchRow($res);
		return  $row['email'];
	}
	function AllReplyMessages($contact_id, $limit, $offset, $is_page)
	{
		$sql = "SELECT * FROM ".$this->tableName1." WHERE contact_id=".$contact_id." ORDER BY id DESC";
		
		// Paggination condition
		if($is_page == true)
			 $sql.=" LIMIT ".$offset.",".$limit;
			 
		$result1= $this->ExecuteQuery($sql);
		$result= $this->FetchAll($result1);
		return $result;
	}
	/*function listRecord($searchtxt, $limit="", $offset="", $is_page)
	{
		echo $searchtxt; echo $limit; echo $offset; exit;
		if($searchtxt != ''){
			$where = "reason LIKE '%".$searchtxt."%' OR name LIKE '%".$searchtxt."%'";
		}else{
			$where = "1";
		}
			
		$sql = "SELECT * FROM ".$this->tableName." where ".$where." ORDER BY entry_on DESC ";

		// Pagination condition
		if($is_page == true)
			 $sql.=" LIMIT ".$offset.",".$limit;
			 
		$result1= $this->ExecuteQuery($sql);
		$result= $this->FetchAll($result1);
		//echo '<pre>';print_r($result);exit;
		return $result;
	}*/
	function listRecord($searchtxt, $limit="", $offset="")
	{
		 
		$fields = array();	
		$tables = array( $this->tableName);
		if($searchtxt==''){
			$where=array();
			
		}else{
			$where[]= " subject LIKE '%".$searchtxt."%' OR name LIKE '%".$searchtxt."%'";	
		}
		$result1 = $this->SelectData( $fields,$tables, $where, $order = array( "entry_on DESC" ), $group=array(),$limit,$offset,0);
		$result= $this->FetchAll( $result1 );
		return $result;
	}
	
	
	function listRecordWithUnread($searchtxt, $limit="", $offset="")
	{
		 
		$fields = array();	
		$tables = array( $this->tableName);
		$where=array("status='unanswered'");
			
		
		$result1 = $this->SelectData( $fields,$tables, $where, $order = array( "entry_on DESC" ), $group=array(),$limit,$offset,0);
		$result= $this->FetchAll( $result1 );
		return $result;
	}
	
function getEmail()
	{
	    $sql="SELECT `email` FROM ".$this->tableName2." WHERE `id`=1";
		$res=$this->ExecuteQuery($sql);  
		$row=$this->FetchRow($res);
		return  $row['email'];
	}
	function AllReplyMessagesById($id,$search,$limit='',$offset='')
	{	
		$fields = array();	
		$tables = array($this->tableName1);
		$clause="contact_id='".$id."'";
		if($search !="")
		{
			$clause.= " AND subject_reply like '%".$search."%'";
		}
		$where=array($clause);		
		
		$result1 = $this->SelectData( $fields,$tables, $where, $order=array(), $group=array(),$limit,$offset,0);
		$result= $this->FetchAll( $result1 );
		return $result;
	}
	function addContactUsReply($array)
	{
		$this->InsertData( $this->tableName1 , $array );		
		$insertId = mysql_insert_id();
		return $insertId;
	}
	function deleteReplyMessages($id)
	{
		$this->DeleteData($this->tableName1,"id",$id);
	}
	function getInquriesCount()
	{
		$fields = array("count(*) as cnt");	
		$tables = array( $this->tableName);
		$where=array("");
		$result1 = $this->SelectData( $fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0 );
		$result= $this->FetchRow( $result1 );
		return $result;
	}
	function getReplydetails($id)
	{
		$fields = array();	
		$tables = array( $this->tableName1 );
		$result1 = $this->SelectData( $fields,$tables, $where = array("id='".$id."'"), $order ="", $group=array(),$limit = "",0,0 );
		$result= $this->FetchRow( $result1 );
		return $result;
	}
	
}

?>
